package com.wzdigit.wms.raw.service.inv;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wzdigit.framework.Shift;
import com.wzdigit.mes.basic.api.BasicDataItemDetailApi;
import com.wzdigit.mes.basic.api.BasicSimpleCodeRuleApi;
import com.wzdigit.mes.basic.api.BasicVendorApi;
import com.wzdigit.mes.basic.dto.DataItemDetailDTO;
import com.wzdigit.mes.basic.dto.VendorDTO;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.inv.SearchInvInitTaskDataPdaRequest;
import com.wzdigit.wms.basic.client.request.inv.SubmitInvInitTaskDataPdaRequest;
import com.wzdigit.wms.basic.client.request.inv.SubmitInvInitTaskDataPdaRequest.SubmitInvInitTaskDataPdaItemRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.basic.LocationApiResponse;
import com.wzdigit.wms.basic.client.response.inv.InvInitTaskDataPdaResponse;
import com.wzdigit.wms.basic.client.response.inv.InvInitTaskQtyResponse;
import com.wzdigit.wms.basic.client.response.inv.InvInitTaskScanSnPdaResponse;
import com.wzdigit.wms.basic.domain.asn.AsnDtl;
import com.wzdigit.wms.basic.domain.asn.AsnDtlGoods;
import com.wzdigit.wms.basic.domain.asn.AsnHeader;
import com.wzdigit.wms.basic.domain.basic.Location;
import com.wzdigit.wms.basic.domain.basic.Sku;
import com.wzdigit.wms.basic.domain.basic.Warehouse;
import com.wzdigit.wms.basic.domain.dtos.basic.QrGoodsDto;
import com.wzdigit.wms.basic.domain.dtos.inventory.PrintSnDataDto;
import com.wzdigit.wms.basic.domain.dtos.inventory.UpdateInvInitTempQtyDto;
import com.wzdigit.wms.basic.domain.dtos.inventory.UpdateInventoryInputDto;
import com.wzdigit.wms.basic.domain.inv.InvInitTemp;
import com.wzdigit.wms.basic.domain.inventory.Goods;
import com.wzdigit.wms.basic.domain.inventory.GoodsLog;
import com.wzdigit.wms.basic.domain.order.TrsnDtl;
import com.wzdigit.wms.basic.domain.order.TrsnDtlGoods;
import com.wzdigit.wms.basic.domain.order.TrsnHeader;
import com.wzdigit.wms.basic.enums.AsnDtlGoodsStatusEnum;
import com.wzdigit.wms.basic.enums.AsnHeaderStatusEnum;
import com.wzdigit.wms.basic.enums.AsnTypeEnum;
import com.wzdigit.wms.basic.enums.GoodsStatusEnum;
import com.wzdigit.wms.basic.enums.OpCodeEnum;
import com.wzdigit.wms.basic.enums.QcStatusEnum;
import com.wzdigit.wms.basic.enums.TransferDtlGoodStatusEnum;
import com.wzdigit.wms.basic.enums.TrsnDtlInvUpdateFlagEnum;
import com.wzdigit.wms.basic.enums.TrsnDtlStatusEnum;
import com.wzdigit.wms.basic.enums.TrsnHeaderStatusEnum;
import com.wzdigit.wms.basic.enums.TrsnNoteTypeEnum;
import com.wzdigit.wms.basic.mapper.core.basic.LocationMapper;
import com.wzdigit.wms.basic.mapper.core.basic.SkuMapper;
import com.wzdigit.wms.basic.mapper.core.basic.WarehouseMapper;
import com.wzdigit.wms.basic.mapper.core.inv.InvInitTempMapper;
import com.wzdigit.wms.basic.mapper.core.order.AsnDtlGoodsMapper;
import com.wzdigit.wms.basic.mapper.core.order.AsnDtlMapper;
import com.wzdigit.wms.basic.mapper.core.order.AsnHeaderMapper;
import com.wzdigit.wms.basic.mapper.core.order.TrsnDtlGoodsMapper;
import com.wzdigit.wms.basic.mapper.core.order.TrsnDtlMapper;
import com.wzdigit.wms.basic.mapper.core.order.TrsnHeaderMapper;
import com.wzdigit.wms.basic.service.code.GoodsSerialCoreService;
import com.wzdigit.wms.basic.service.code.InventoryService;
import com.wzdigit.wms.basic.service.code.QrCodeParsingService;
import com.wzdigit.wms.basic.service.code.WhsPermService;
import com.wzdigit.wms.basic.util.DateUtils;
import com.wzdigit.wms.basic.util.HttpSessionUtil;
import com.wzdigit.wms.basic.util.WrapperUtil;
import com.wzdigit.wms.basic.util.common.SeparatorString;
import com.wzdigit.wms.raw.service.wmscommon.core.GoodsService;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 库存初始化---PDA端
 * 
 * @author Administrator
 *
 */
@Service
public class InvInitTaskDataPdaService {
	
	@Autowired
	private SkuMapper skuMapper;
	
	@Autowired
	private LocationMapper locationMapper;
	
	@Autowired
	private WarehouseMapper warehouseMapper;

	@Autowired
	private InvInitTempMapper invInitTempMapper;
	
	@Autowired
	private AsnHeaderMapper asnHeaderMapper;
	
	@Autowired
	private AsnDtlMapper asnDtlMapper;
	
	@Autowired
	private AsnDtlGoodsMapper asnDtlGoodsMapper;
	
	@Autowired
    private TrsnHeaderMapper trsnHeaderMapper;
	
	@Autowired
    private TrsnDtlMapper trsnDtlMapper;

	@Autowired
    private TrsnDtlGoodsMapper trsnDtlGoodsMapper;
	
	@Autowired
	private QrCodeParsingService qrCodeParsingService;
	
	@Autowired
	private GoodsService goodsService;
	
	@Autowired
	private InventoryService inventoryService;
	
	@Autowired
	private WhsPermService whsCodeService;
	
	@Autowired
	private GoodsSerialCoreService goodsSerialCoreService;
	
	@Reference(version = "${dubbo.consumers.basic.data.item.detail.api:1.0.0}", check = false, timeout = 300000)
    private BasicDataItemDetailApi dataItemDetailApi;
	
	@Reference(version = "${dubbo.consumers.basic.simple.code.rule.api:1.0.0}", check = false, timeout = 300000)
    private BasicSimpleCodeRuleApi basicSimpleCodeRuleApi;
	
	@Reference(version = "${dubbo.consumers.vendor.api:1.0.0}", check = false)
    private BasicVendorApi basicVendorApi;
	
	@Value(value = "${default_owner.code}")
    private String DEFAULT_OWNER;

    @Value(value = "${default_owner.name}")
    private String DEFAULT_OWNER_NAME;
    
    private static final String ASN_R98 = "ASN_R98";			// 创建库存初始化入库单号编码
    private static final String TRSN_ORDER = "TRSN_ORDER";		// 创建凭证编码

    
	public PagerInfo<List<InvInitTaskDataPdaResponse>> selectData(SearchInvInitTaskDataPdaRequest dto, Pager<?> pager, 
			HttpServletRequest servletRequest) {
		// 当前用户权限的仓库
        List<Integer> whsIdList = whsCodeService.getWareHouseIds(servletRequest);
        
		if(dto.getQrRuleParam() != null) {
			QrGoodsDto qrGoodsDto = qrCodeParsingService.initializeGoodsQrCode(dto.getQrRuleParam());
			dto.setSkuCode(qrGoodsDto.getSkuCode());
		}
		Page<?> page = PageHelper.offsetPage(pager.getSkipCount(), pager.getMaxResultCount());
        PagerInfo<List<InvInitTaskDataPdaResponse>> pagerInfo = new PagerInfo<>();
        List<InvInitTaskDataPdaResponse> list = invInitTempMapper.selectInvInitTaskDataPda(dto, whsIdList);
        pagerInfo.setItems(list);
        pagerInfo.setTotalCount(page.getTotal());
		return pagerInfo;
	}
	
	
	public InvInitTaskQtyResponse refreshQty(Integer invInitId) {
		InvInitTaskQtyResponse vo = invInitTempMapper.selectInvInitTaskQty(invInitId);
		return vo;
	}
	
	
	public LocationApiResponse scanLoc(Integer whsId, String whsName, String locCode) {
		Location loc = locationMapper.selectOneByLocCode(locCode);
		if(loc == null) {
			Shift.fatal("库位不存在");
		}
		Warehouse whs = warehouseMapper.selectOneByLocCode(locCode);
		if(!Objects.equals(whsId, whs.getWhsId())) {
			Shift.fatal(String.format("库位[%s]不属于仓库[%s]！", locCode, whsName));
		}
		LocationApiResponse vo = new LocationApiResponse();
		BeanUtils.copyProperties(loc, vo);
		return vo;
	}
	
	
	public InvInitTaskScanSnPdaResponse scan(Integer invInitId, String qrRuleParam) {
		QrGoodsDto qrGoodsDto = qrCodeParsingService.initializeGoodsQrCode(qrRuleParam);
		Goods goods = goodsService.selectBySn(qrGoodsDto.getGoodsSn());
		if(goods != null) {
			Shift.fatal("该SN已存在，不可重复初始化");
		}
		
		InvInitTemp invInitTemp = invInitTempMapper.selectById(invInitId);
		if(!Objects.equals(invInitTemp.getSkuCode(), qrGoodsDto.getSkuCode())) {
			Shift.fatal(String.format("SN[%s]物料编码[%s]同当前库存数据物料编码[%s]不同，不可操作", qrGoodsDto.getGoodsSn(), qrGoodsDto.getSkuCode(), invInitTemp.getSkuCode()));
		}
		
		InvInitTaskScanSnPdaResponse vo = new InvInitTaskScanSnPdaResponse();
		BeanUtils.copyProperties(qrGoodsDto, vo);
		return vo;
	}
	
	
	/**
	 * @param qrRuleParam
	 * @param removeQty
	 * @return	返回打印机打印新条码的数据
	 */
	public PrintSnDataDto split(String qrRuleParam, BigDecimal removeQty, String whsName) {
		QrGoodsDto qrGoodsDto = qrCodeParsingService.initializeGoodsQrCode(qrRuleParam);
		
		// 新数量
		BigDecimal qty = qrGoodsDto.getQty().subtract(removeQty);
		
		// 新条码
		String sn = goodsSerialCoreService.generateGoodsSn(qrGoodsDto.getGoodsSn());
		
		PrintSnDataDto vo = new PrintSnDataDto();
		BeanUtils.copyProperties(qrGoodsDto, vo);
		vo.setQty(qty);
		vo.setGoodsSn(sn);
		vo.setWhsName(whsName);
		vo.setPrintTime(DateUtils.format(new Date(), "yyyy/MM/dd HH:mm:ss"));
		vo.setQrCodeStr(qrCodeParsingService.generateGoodsQrCode(qrRuleParam, sn, qty));
		return vo;
	}
	
	
	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	public Result<String> submit(SubmitInvInitTaskDataPdaRequest dto, 
			HttpServletRequest servletRequest) throws Exception {
		// 【行级锁】控制到每个初始化单
		QueryWrapper<InvInitTemp> lockWrapper = WrapperUtil.lock(InvInitTemp.class);
		lockWrapper.eq("INV_INIT_ID", dto.getInvInitId());
		InvInitTemp invInitTemp = invInitTempMapper.selectOne(lockWrapper);
				
		// 已扫描的SN集合
		List<SubmitInvInitTaskDataPdaItemRequest> allItemList = dto.getItemList();
		List<String> snList = allItemList.stream().map(t -> t.getGoodsSn()).collect(Collectors.toList());
		
		// 筛选哪些SN已经入库过
		List<Goods> goodsList = goodsService.selectBySnList(snList);
		if(goodsList.size() > 0) {
			snList = goodsList.stream().map(t -> t.getGoodsSn()).collect(Collectors.toList());
			return new Result<>(false, String.format("SN%s已存在，不可重复初始化", snList.toString()));
		}
		
		// 总操作数
		BigDecimal allQty = allItemList.stream().map(t -> t.getQty()).reduce(BigDecimal.ZERO, BigDecimal::add);
		if (Objects.equals("5520", invInitTemp.getFactory())) {
			if ((invInitTemp.getActQty().add(allQty)).compareTo(invInitTemp.getBookQty()) == 1) {
				Shift.fatal("实盘数量不能超出账目数量");
			}
		}
		
		// 默认货主
		DataItemDetailDTO dataItem = dataItemDetailApi.getDataItemDetailByPCodeAndName(DEFAULT_OWNER, DEFAULT_OWNER_NAME);
		Integer owner = Integer.parseInt(dataItem.getItemValue());
		
		// 用户名、当前时间
		String loginName = HttpSessionUtil.getLoginName(servletRequest);
		Date date = new Date();
		
		// 物料
		Sku sku = skuMapper.selectById(invInitTemp.getSkuId());
		
		// 根据【供应商分组】入库
		Map<Integer, List<SubmitInvInitTaskDataPdaItemRequest>> vendorMap = allItemList.stream().collect(Collectors.groupingBy(t -> t.getVendorId(), Collectors.toList()));
		List<String> asnNoList = new ArrayList<>(vendorMap.size());
		goodsList = new ArrayList<Goods>(allItemList.size());
		for (Integer vendorKey : vendorMap.keySet()) {
			List<SubmitInvInitTaskDataPdaItemRequest> itemList = vendorMap.get(vendorKey);
			BigDecimal qty = itemList.stream().map(t -> t.getQty()).reduce(BigDecimal.ZERO, BigDecimal::add);
			
			String asnNo = basicSimpleCodeRuleApi.GenerateCode(ASN_R98);
			asnNoList.add(asnNo);
			
			// 创建收货单表头
			AsnHeader asnHeader = new AsnHeader();
			asnHeader.setAsnNo(asnNo);
			asnHeader.setAsnType(AsnTypeEnum.R98.code);
			asnHeader.setVendorId(vendorKey);
			asnHeader.setOwner(owner);
			asnHeader.setSourceNo(invInitTemp.getInvInitId().toString());
			asnHeader.setPriority(0);
			asnHeader.setFactory(invInitTemp.getFactory());
			asnHeader.setToWhs(invInitTemp.getWhs());
			asnHeader.setReleaseStatus(1);
			asnHeader.setQcStatus(QcStatusEnum.未创建质检单.getCode());
			asnHeader.setStatus(AsnHeaderStatusEnum.FINISH.code);
			asnHeader.setToBusinessMan(loginName);
			asnHeader.setActualarrivetime(date);
			asnHeader.setCreateUser(loginName);
			asnHeader.setCreateTime(date);
			asnHeaderMapper.insert(asnHeader);
			
			// 创建收货单明细
			AsnDtl asnDtl = new AsnDtl();
			asnDtl.setAsnNo(asnNo);
			asnDtl.setAsnLineNo("1");
			asnDtl.setAsnHeaderId(asnHeader.getAsnHeaderId());
			asnDtl.setSkuId(invInitTemp.getSkuId());
			asnDtl.setSkuDesc(sku.getDescCn());
			asnDtl.setOrderQty(qty);
			asnDtl.setOrderQtyEa(qty);
			asnDtl.setRcvQty(qty);
			asnDtl.setRcvQtyEa(qty);
			asnDtl.setPutawayQty(qty);
			asnDtl.setPutawayQtyEa(qty);
			asnDtl.setUnit(sku.getBaseUnit());
			asnDtl.setRcvTime(date);
			asnDtl.setWhsId(invInitTemp.getWhs());
			asnDtl.setQcStatus(QcStatusEnum.未创建质检单.getCode());
			asnDtl.setQaStatus(invInitTemp.getQaStatus());
			asnDtl.setStatus(AsnHeaderStatusEnum.FINISH.code);
			asnDtl.setPutawayFlag(1);
			asnDtl.setCreateUser(loginName);
			asnDtl.setCreateTime(date);
			asnDtl.setPutawayStatus(itemList.stream().filter(t -> t.getLocId() != null).count() == 0 ? 0 : 1);
			asnDtlMapper.insert(asnDtl);
			
			// 【入库凭证--单头】
			TrsnHeader trsnHeader = new TrsnHeader();
			trsnHeader.setNoteType(TrsnNoteTypeEnum.IN_STORAGE.getCode());
			trsnHeader.setStatus(TrsnHeaderStatusEnum.CMP.code);
			trsnHeader.setNoteNo(basicSimpleCodeRuleApi.GenerateCode(TRSN_ORDER));
			trsnHeader.setOwner(asnHeader.getOwner());
			trsnHeader.setFactory(asnHeader.getFactory());
			trsnHeader.setVendorId(asnHeader.getVendorId());
			trsnHeader.setFmDoc(asnHeader.getAsnNo());
			trsnHeader.setWhsId(asnDtl.getWhsId());
			trsnHeader.setCreateUser(loginName);
			trsnHeader.setCreateTime(date);
			trsnHeaderMapper.insert(trsnHeader);
			
			// 【入库凭证--明细】
			TrsnDtl trsnDtl = new TrsnDtl();
			trsnDtl.setNoteLineNo("1");
			trsnDtl.setInvUpdateFlag(TrsnDtlInvUpdateFlagEnum.IN.code);
			trsnDtl.setStatus(TrsnDtlStatusEnum.CMP.code);
			trsnDtl.setUnit(asnDtl.getUnit());
			trsnDtl.setSkuId(asnDtl.getSkuId());
			trsnDtl.setQty(qty);
			trsnDtl.setFmDoc(asnHeader.getAsnNo());
			trsnDtl.setQaStatus(asnDtl.getQaStatus());
			trsnDtl.setFmDocLine(asnDtl.getAsnLineNo());
			trsnDtl.setNoteNo(trsnHeader.getNoteNo());
			trsnDtl.setTrsnHeaderId(trsnHeader.getTrsnHeaderId());
			trsnDtl.setCreateUser(loginName);
			trsnDtl.setCreateTime(date);
			trsnDtlMapper.insert(trsnDtl);
			
			for (SubmitInvInitTaskDataPdaItemRequest item : itemList) {
				// 新增GOODS
				Goods goods = new Goods();
				goods.setGoodsSn(item.getGoodsSn());
				goods.setOwner(owner);
				goods.setSkuId(invInitTemp.getSkuId());
				goods.setFactory(invInitTemp.getFactory());
				goods.setVenLotno(item.getVenLotno());
				goods.setPrdLot(item.getPrdLot());
				goods.setDatecode(item.getDatecode());
				goods.setStockInDate(date);
				goods.setQaStatus(invInitTemp.getQaStatus());
				goods.setQty(item.getQty());
				goods.setUnit(sku.getBaseUnit());
				goods.setSnType(5);
				goods.setIsOriginalPackage(1);
				goods.setWhsId(asnDtl.getWhsId());
				goods.setLocId(item.getLocId());
				goods.setCreateUser(loginName);
				goods.setCreateTime(date);
				goods.setStatus(item.getLocId() == null ? GoodsStatusEnum.WAIT_UP.code : GoodsStatusEnum.HAS_UP.code);
				if(item.getVendorCode() == null) {
					VendorDTO vendorDTO = basicVendorApi.getVendorById(item.getVendorId());
					goods.setVendorCode(vendorDTO.getVendorCode());
				} else {
					goods.setVendorCode(item.getVendorCode());
				}
				try {
					goodsService.insertGoods(goods);
				} catch (DuplicateKeyException e) {
					// 违反SN的唯一索引限制条件
					Shift.fatal(String.format("SN[%s]已存在，不可重复初始化", goods.getGoodsSn()));
				}
				goodsList.add(goods);
				
				// 新增GOODS_LOG【建档】
				GoodsLog goodsLog1 = new GoodsLog();
				goodsLog1.setInvTransId(trsnDtl.getTrsnDtlId());
				goodsLog1.setGoodsSn(goods.getGoodsSn());
				goodsLog1.setOpCode(OpCodeEnum.CREATE.getCode());
				goodsLog1.setReason(OpCodeEnum.CREATE.getEnumName());
				goodsLog1.setOperator(OpCodeEnum.CREATE.getEnumName());
				goodsLog1.setUpdateContent(JSON.toJSONString(goods));
				goodsLog1.setCreateUser(loginName);
				goodsLog1.setCreateTime(date);
				goodsService.insertGoodsLog(goodsLog1);
				
				// 新增GOODS_LOG【入库】
				GoodsLog goodsLog2 = new GoodsLog();
				goodsLog2.setInvTransId(trsnDtl.getTrsnDtlId());
				goodsLog2.setGoodsSn(goods.getGoodsSn());
				goodsLog2.setOpCode(OpCodeEnum.RECEIVE.getCode());
				goodsLog2.setReason(OpCodeEnum.RECEIVE.getEnumName());
				goodsLog2.setOperator(OpCodeEnum.RECEIVE.getEnumName());
				goodsLog2.setUpdateContent(JSON.toJSONString(goods));
				goodsLog2.setCreateUser(loginName);
				goodsLog2.setCreateTime(date);
				goodsService.insertGoodsLog(goodsLog2);
				
				// 创建收货单明细--货品清单
				AsnDtlGoods asnDtlGoods = new AsnDtlGoods();
				asnDtlGoods.setAsnDtlId(asnDtl.getAsnDtlId());
				asnDtlGoods.setGoodsSn(goods.getGoodsSn());
				asnDtlGoods.setGoodsId(goods.getGoodsId());
				asnDtlGoods.setQaStatus(asnDtl.getQaStatus());
				asnDtlGoods.setQty(goods.getQty());
				asnDtlGoods.setStatus(AsnDtlGoodsStatusEnum.HAS_RECEIVE.code);
				asnDtlGoods.setCreateUser(loginName);
				asnDtlGoods.setCreateTime(date);
				asnDtlGoodsMapper.insert(asnDtlGoods);
							
				// 【入库凭证--明细goods】
				TrsnDtlGoods trsnDtlGoods = new TrsnDtlGoods();
				trsnDtlGoods.setTrsnDtlId(trsnDtl.getTrsnDtlId());
				trsnDtlGoods.setGoodsSn(goods.getGoodsSn());
				trsnDtlGoods.setGoodsId(goods.getGoodsId());
				trsnDtlGoods.setQty(goods.getQty());
				trsnDtlGoods.setQaStatus(goods.getQaStatus());
				trsnDtlGoods.setStatus(TransferDtlGoodStatusEnum.HAS_RECEIVE.code);
				trsnDtlGoods.setCreateTime(date);
				trsnDtlGoods.setCreateUser(loginName);
				trsnDtlGoodsMapper.insert(trsnDtlGoods);
			}
		}
		
		// 【更新库存】
		Map<String, List<Goods>> goodsMap = goodsList.stream().collect(Collectors.groupingBy(t ->
			(t.getWhsId() + "#" + t.getLocId() + "#" + t.getOwner() + "#" +
			t.getSkuId() + "#" + t.getFactory() + "#" + t.getVendorId() + "#" +
		    t.getLotno() + "#" + t.getInvAttr() + "#" + t.getQaStatus() + "#"), Collectors.toList()));
		for (String goodsKey : goodsMap.keySet()) {
			List<Goods> goodsInvList = goodsMap.get(goodsKey);
			Goods goodsInv = goodsInvList.get(0);
			BigDecimal goodsQty = goodsInvList.stream().map(t -> t.getQty()).reduce(BigDecimal.ZERO, BigDecimal::add);

			UpdateInventoryInputDto input = new UpdateInventoryInputDto();
			input.setWhsId(goodsInv.getWhsId()); 		// 仓库
			input.setLocId(goodsInv.getLocId()); 		// 库位
			input.setOwner(goodsInv.getOwner()); 		// 货主
			input.setSkuId(goodsInv.getSkuId()); 		// 物料
			input.setFactory(goodsInv.getFactory()); 	// 组织
			input.setLotNo(goodsInv.getLotno()); 		// 批次号
			input.setVendorId(goodsInv.getVendorId()); 	// 供应商
			input.setInvAttr(goodsInv.getInvAttr()); 	// 库存性质
			input.setQaStatus(goodsInv.getQaStatus()); 	// QA状态
			input.setUnit(goodsInv.getUnit()); 			// 单位
			input.setQty(goodsQty);
			input.setOperator(loginName);
			
			inventoryService.updateLocInventroy(input);
			inventoryService.updateZoneInventory(input);
		}
		
		// 回写库存初始化数据
		UpdateInvInitTempQtyDto qtyDto = new UpdateInvInitTempQtyDto();
		qtyDto.setInvInitId(invInitTemp.getInvInitId());
		qtyDto.setActQty(allQty);
		qtyDto.setOperator(loginName);
		invInitTempMapper.updateQty(qtyDto);
		// Shift.fatal("回滚事务！");
		return new Result<>(StringUtils.join(asnNoList.toArray(), ","));
	}
}
